var myKeyboardEvents = new Keyboard({});

var sflRequest;

var mtable;
var cmtable;

function order_silniki_by(){
    
}


function get_silniki_full_list(){
    
//    var filter_params={
//        search: $('magazyn_searcher').value,
//        ref_no: $('inp_ref_no').value,
//        order_by: $('inp_order_by').value,
//        order_how: $('inp_order_how').value,
//        obroty_od: $('obroty_od').value,
//        obroty_do: $('obroty_do').value,
//        obroty: $('obroty').value,
//        moc_od: $('moc_od').value,
//        moc_do: $('moc_do').value,
//        stan_zerowy: $('inp_stan_zerowy').checked,
//        zaznacz_progresywne: $('inp_zaznacz_progresywne').checked,
//        no_model_group: $('no_model_group_inp').checked
//    };
    
    var filter_params = {};
    
    $('magazyn_menu').getElements('input,select').each(function(el){
        if(el.type == 'checkbox'){
            filter_params[el.name] = el.checked;
        }else{
            filter_params[el.name] = el.value;
        }
    });

    filter_params.jednofaz=false;
    filter_params.trojfaz=false;
    if($('jednofaz_inp').checked){
        filter_params.jednofaz=true;
    }
    if($('trojfaz_inp').checked){
        filter_params.trojfaz=true;
    }
    
    if(sflRequest){
        sflRequest.cancel();
    }
    
    var a = new AlertInfo();
    a.show();
    
    sflRequest = new Request.HTML({
        url: BASE_URL+'/magazynajax/motortypeslist',
        method: 'post',
        update: 'magazyn_content',
        onSuccess: function(response){

            mtable = new HtmlTable('motor_list',{
                sortable: true,
                onSort: function(){
                }
            });
            
            a.hide();
            
        },
        onFailure: function(response){
            $('magazyn_content').set('html', response);
            a.error();
        },
        onComplete: function(){
            
        },
        onCancel: function(){
            a.hide();
        }
    }).send('filter_params='+JSON.encode(filter_params));
}

var last_clicked_elem;

function get_types_motors(elem, id_acmotor_type, id_motor_mocowania){
    var tr = Helper.getTr(elem);
    var tr_active_F=false;

    if(tr.hasClass('tr-active') && elem==last_clicked_elem){
        tr_active_F=true;
    }
    last_clicked_elem=elem;

    $('motor_list').getElements('tr').each(function(el){
        el.removeClass('tr-active');
    });
    
    $('magazyn_content').getElements('.details-tr').each(function(el){
        el.destroy();
    });

    if(tr_active_F){
        mtable.restore(cmtable);
        
        return;
    }else{
        cmtable = mtable.serialize();
//        mtable.disableSort();
    }
    
    var a = new AlertInfo();
    a.show();

    tr.addClass('tr-active');

    var new_tr = new Element('tr', {'class':'details-tr tr-active'}).inject(tr, 'after');
    new Element('td',{'html':'','class':'trans-td'}).inject(new_tr);
    var new_td = new Element('td',{colspan: '50'}).inject(new_tr);

    var filter_params={
        id_acmotor_type: id_acmotor_type,
        id_motor_mocowania: id_motor_mocowania
    };

    var myRequest = new Request({
        url: BASE_URL+'/magazynajax/get_types_motors',
        method: 'post',
        onSuccess: function(response){
            new_td.set('html', response);
            a.hide();

        },
        onFailure: function(response){
            $('magazyn_content').set('html', response);
            a.error();
        },
        onComplete: function(response){
        },
        onCancel: function(){
            a.hide();
        }
    }).send('filter_params='+JSON.encode(filter_params));
}


function delete_silnik(elem, id){
    
    if(confirm('Na pewno chcesz usunąć ?')){

        var tr = Helper.getTr(elem);
        tr.getElements('td').each(function(td){
            td.setStyle('background', 'transparent');
        })
        
        var fx = new Fx.Morph(tr, {
            duration: 550,
            transition: 'sine:in',
            onComplete: function(){
                tr.destroy();
            }
        });

        fx.start({
            'background-color': ['#fff','#d00']
        })

        var a = new AlertInfo();
        a.show();
        
        var myRequest = new Request({
            url: BASE_URL+'/magazynajax/del_motor/'+id,
            method: 'post',
            onSuccess: function(response){
                a.hide();
            },
            onFailure: function(response, responseBody){
//                $('magazyn_content').set('html', response.responseText
                a.error();
            },
            onComplete: function(response){

            },
            onCancel: function(){
                a.hide();
            }
        }).send();

    }
}

function gen_pdf(butt){
//    $('ramka_php').location.href=BASE_URL+'/magazynajax/motorlist_report';

//    new IFrame($('ramka_php'), {
//        'src': BASE_URL+'/magazynajax/motorlist_report',
//        load: function(){
//            this.src='#';
//        }
//    });

    var form = $('magazyn_menu_form');
    form.set('action', BASE_URL+'/magazynajax/motorlist_report');
    form.set('target', '_blank');
    form.submit();
//    form.set('target', 'ramka_php');
}


function add_to_cart(elem, id){
    
    var a = new AlertInfo();
    a.show();

    var myRequest = new Request({
        url: BASE_URL+'/magazynajax/addcart_motor/'+id,
        method: 'post',
        onSuccess: function(response){
            a.hide();
            var tr = Helper.getTr(elem);
            tr.addClass('in-cart');
            elem.removeProperty('onclick');
            elem.addClass('in-cart');
        },
        onFailure: function(response, responseBody){
            //$('magazyn_content').set('html', response.responseText);
            a.error();
        },
        onComplete: function(response){

        },
        onCancel: function(){
        }
    }).send();
}



var prizeCalc = new Class({
    Implements: Options,
    motorTypeId: null,
    cont: null,
    elem: null,
    options: {
        container: null
    },
    initialize: function(options, motorTypeId, elem) {
        this.setOptions({
            container: $(document.body)
        });
        this.setOptions(options);
        this.motorTypeId = motorTypeId;
        this.elem = elem;
    },
    display: function(){
        var self = this;
        var container = self.options.container;
        if(self.elem)self.elem.hide();
        
        var wrap = new Element('div', {'class':'prizeCalc-wrap'});
        wrap.inject(container, 'bottom');
        var closee = new Element('div', {'class':'prizeCalc-close', 'html':'X'}).inject(wrap);
        var cont = new Element('div', {'class':'prizeCalc-cont'}).inject(wrap);
        
        Helper.putAjaxLoader(cont);
        
        var myRequest = new Request({
            url: BASE_URL+'/magazynajax/getcalccontent/'+self.motorTypeId,
            method: 'post',
            onSuccess: function(response){
                cont.set('html', response);
                self.cont = cont;
                var butt = cont.getElement('.prizeCalc-przeliczButton');
                butt.addEvent('click', function(e){
                    self.updateRes();
                });
                var winsize = window.getSize();
                var wrapsize = wrap.getSize();
                var myDrag = new Drag(wrap, {
                    snap: 0,
                    limit:{x:[0,winsize.x-wrapsize.x],y:[0,winsize.y-wrapsize.y]},
                    handle: cont.getElement('.prizeCalc-header'),
                    onSnap: function(el){
                        wrap.addClass('dragging');
                        var indx = self.getMaxZindex();
                        wrap.setStyle('z-index', indx+1);
                    },
                    onComplete: function(el){
                        wrap.removeClass('dragging');
                    }
                });
            },
            onFailure: function(response){
                cont.set('html', response.response);
            },
            onComplete: function(response){
                closee.addEvent('click', function(e){
                    wrap.destroy();
                    if(self.elem)self.elem.show();
                })
            },
            onCancel: function(){
            }
        }).send();
    },
    updateRes: function(){
        var self = this;
        var params = {
            'marza': self.cont.getElement('.prizeCalc-inp-marza').value
        };
        var json = JSON.encode(params);
        var tabPlace = self.cont.getElement('.prizeCalc-prizeTable-wrap');
        Helper.putAjaxLoader(tabPlace);
            
        var myRequest = new Request({
            url: BASE_URL+'/magazynajax/getcalccontenttable/'+self.motorTypeId,
            method: 'post',
            onSuccess: function(response){
                tabPlace.set('html', response);
            },
            onFailure: function(response){
                tabPlace.set('html', response.response);
            },
            onComplete: function(response){

            },
            onCancel: function(){
            }
        }).send('json='+json);
    },
    getMaxZindex: function(){
        var self = this;
        var indx = 0;
        self.options.container.getElements('.prizeCalc-wrap').each(function(el){
            var tmp = parseInt(el.getStyle('z-index'));
            if(tmp > indx){
                indx = tmp;
            }
        })
        return indx;
    }
    
});